import "./baseInit.js";
import './Mail.js';
import './Warehouse.js';
import "./screenUiScale.js";

// const remoteListen = (() => {
//     const cb = new Map();
//     let counter = 0;
//     remoteChannel.events.on("client", (event) => {
//         for ([id, data] of cb) {
//             if (data.type !== event.type) {
//                 continue;
//             }
//             console.log("test")
//             data.callback(event);
//         }
        
//     })
//     return function (type, callback) {
//         const id = counter++;
//         cb.set(id, {
//             type,
//             callback
//         });
//         return function () {
//             if (!cb.has(id)) {
//                 return;
//             }
//             cb.delete(id);            
//         };
//     }
// })();

// remoteListen("shake_hand", () => { console.warn("测试成功") })

const v = "Desktop" == navigator.getDeviceInfo().deviceType;

let connect, init;

const dxname = ui.findChildByName('敌袭名称');
const dxtime = ui.findChildByName('敌袭时间');

const dixi = ui.findChildByName('敌袭')
const textList = [
    '敌袭名称',
    '敌袭时间',

    
]

remoteChannel.events.on("client", async(event) => {
    if (event.type === "shake_hand") {
        connect = true;
    }
});

function waitForConnectChange() {
    const max = 100;
    let count = 0;
    return new Promise((resolve) => {
        const timer = setInterval(() => {
            if (count > max) {
                clearInterval(timer);
                resolve(connect);
            };
            if (connect === true) {
                clearInterval(timer);
                resolve(connect);
            };
            count++;
        }, 64);
    });
}

const shake_hand = async () => {
    connect = false;
    remoteChannel.sendServerEvent({ type: "shake_hand" });
    await waitForConnectChange().then((connect) => {
        return;
    });
    const ISC = connect;
    connect = false;
    return ISC;
};

const client_init = async function() {
    console.log("正在尝试与服务端连接...");
    const connected = await shake_hand();
    connected ? console.log("连接成功") : console.log("连接失败 (请求超时: 6400ms)");
    clearTimeout(init);
    try {
        input.lockPointer();
    } catch(error) {
        null;
    };
}();

(async () => {
    setTimeout(async () => {
        let count = 0;
        while (count++ < 5) {
            console.log("Trying to import client script...");
            count > 1 ? await sleep(1000) : null;
            try {
                console.log("Import client script sucessfully");
                init = setTimeout(client_init, 1000);
                return;
            } catch (error) {
                console.log(`Failed: Count[${count}]`);
                await sleep(500);
            };
        };
        console.log("Failed to import client script");
    }, 2000);
    return;
})();

remoteChannel.events.on('client', (arg) => {
    if (arg.type == 'first') {
        textList.forEach((v, i) => {
            dixi.findChildByName(v).textContent = arg.data[i]
        })
    }if (arg.type == 'coinandexp') {
        textList1.forEach((v, i) => {
        xs.findChildByName(v).textContent = arg.data[i]
        })
    }
})



/*const coin = ui.findChildByName('kuake');
const exp = ui.findChildByName('exp1');

const xs = ui.findChildByName('xs')
const textList1 = [
    'kuake',
    'exp1',

    
]*/



const yijian = ui.findChildByName('yijian');
yijian.pointerEventBehavior = PointerEventBehavior.ENABLE;
yijian.events.on("pointerup", () => {
    remoteChannel.sendServerEvent({
        type: 'yijian-click',
        data: { msg: "yijian成功加载" }
    });
});

